{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b63b8343",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的余额：6666\n",
      "---------  欢迎选购  ----------\n",
      "退出购物页面请输入‘q’\n",
      "商品编号：0，商品名称：咖啡，商品价格：7\n",
      "商品编号：1，商品名称：泡泡糖，商品价格：6\n",
      "商品编号：2，商品名称：巧乐兹，商品价格：4\n",
      "商品编号：3，商品名称：车仔面，商品价格：6\n",
      "请输入购买的商品编号：0\n",
      "[咖啡]已加入购物车，您的余额还有:6659元\n",
      "---------  欢迎选购  ----------\n",
      "退出购物页面请输入‘q’\n",
      "商品编号：0，商品名称：咖啡，商品价格：7\n",
      "商品编号：1，商品名称：泡泡糖，商品价格：6\n",
      "商品编号：2，商品名称：巧乐兹，商品价格：4\n",
      "商品编号：3，商品名称：车仔面，商品价格：6\n",
      "请输入购买的商品编号：2\n",
      "[巧乐兹]已加入购物车，您的余额还有:6655元\n",
      "---------  欢迎选购  ----------\n",
      "退出购物页面请输入‘q’\n",
      "商品编号：0，商品名称：咖啡，商品价格：7\n",
      "商品编号：1，商品名称：泡泡糖，商品价格：6\n",
      "商品编号：2，商品名称：巧乐兹，商品价格：4\n",
      "商品编号：3，商品名称：车仔面，商品价格：6\n",
      "请输入购买的商品编号：3\n",
      "[车仔面]已加入购物车，您的余额还有:6649元\n",
      "---------  欢迎选购  ----------\n",
      "退出购物页面请输入‘q’\n",
      "商品编号：0，商品名称：咖啡，商品价格：7\n",
      "商品编号：1，商品名称：泡泡糖，商品价格：6\n",
      "商品编号：2，商品名称：巧乐兹，商品价格：4\n",
      "商品编号：3，商品名称：车仔面，商品价格：6\n",
      "请输入购买的商品编号：q\n",
      "您购买了： [['咖啡', 7], ['巧乐兹', 4], ['车仔面', 6]]\n",
      "您的余额还有： 6649\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 商品的数据信息\n",
    "good_list = [\n",
    "    # [商品名称，商品价格]\n",
    "    ['咖啡', 7],\n",
    "    ['泡泡糖', 6],\n",
    "    ['巧乐兹', 4],\n",
    "    ['车仔面', 6]\n",
    "]\n",
    "\n",
    "user_shopping_car = [\n",
    "\n",
    "]\n",
    "# 用户输入 使用input , input的使用，结果是一个字符串\n",
    "Alipay = int(input(\"请输入您的余额：\"))\n",
    "\n",
    "while True:\n",
    "    print(\"---------  欢迎选购  ----------\")\n",
    "    print(\"退出购物页面请输入‘q’\")\n",
    "    # 循环遍历商品列表，将商品信息按照一定格式输出\n",
    "    # 一定格式： 商品编号：xx， 商品名称：xx，商品价格：xx\n",
    "\n",
    "    for i in range(len(good_list)):  # range会根据商品列表的长度变化而变化\n",
    "        print(\"商品编号：%s，商品名称：%s，商品价格：%s\" % (i, good_list[i][0], good_list[i][1]))\n",
    "\n",
    "    # 让用户输入商品编号进行购买\n",
    "    # 用户输入的内容是否规范（用户买的东西有无？）\n",
    "\n",
    "    # 思考：用户输入除了数值外，还有可能输入不是数值\n",
    "\n",
    "    choice = input(\"请输入购买的商品编号：\")\n",
    "\n",
    "    if choice.isdigit():  # isdigit 字符串里面的内容是否为数值\n",
    "        choice = int(choice)\n",
    "        if choice > len(good_list):\n",
    "            print(\"您选择的商品不存在！\")\n",
    "        else:\n",
    "            #   如果有，判断金额是否足够:用户金额 Alipay ，商品金额good_list[choice][1]\n",
    "            if Alipay > good_list[choice][1]:\n",
    "                # 用户余额足够，则增加商品到购物车，扣除余额\n",
    "                user_shopping_car.append(good_list[choice])\n",
    "                Alipay = Alipay - good_list[choice][1]\n",
    "                print(\"[%s]已加入购物车，您的余额还有:%s元\" % (good_list[choice][0], Alipay))\n",
    "            else:\n",
    "                print(\"您的余额不足！\")\n",
    "    elif choice == \"q\":\n",
    "        # 用户退出购买\n",
    "        print(\"您购买了：\", user_shopping_car)\n",
    "        print(\"您的余额还有：\", Alipay)\n",
    "        break\n",
    "    else:\n",
    "        print(\"请输入商品编号！！！\")\n",
    "    #   如果没有该商品，返回没有"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03bee64c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
